<?php

namespace app\shop\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\shop\model\MemberDistribution as MemberDistributionModel;
use app\shop\model\MemberDistributionOrder as MemberDistributionOrderModel;

class DistributionOrder extends Controller
{
    public function index()
    {
        $model = new MemberDistributionModel();
        $id = Session::get('member_id');

        $level = $model->where('referrals_id', $id)->value('level');   //查找此会员的级别定位

        $condition = ['sun_member_distribution.hierarchy' => ['like', '%,' . $id . ',%']];

        $data = $model->where($condition)->where(['sun_member_distribution.level' => ['elt', ($level + 2)],])
            ->with([
                'member',
                'memberDistributionOrder' => function ($e) {
                    $e->where('distribution_id', Session::get('member_id'));  //分销订单上级会员id是当前会员
                }
            ])->order(['create_time' => 'desc'])->select();

        if ($data) {
            //去除没有会员信息和分销订单的数据
            foreach ($data as $key => &$item) {
                if (!$item->member || !$item->memberDistributionOrder) {
                    unset($data[$key]);
                } else {
                    foreach ($item->memberDistributionOrder as $o) {
                        $o->memberOrder;    //分销订单关联会员已产生佣金的订单  [状态为3,4的订单]
                    }
                    $flag = 0;
                    foreach ($item->memberDistributionOrder as $_key => $_item) {
                        if (!is_null($_item->memberOrder)) {
                            $flag = 1;
                        }
                    }
                    if (!$flag) {
                        unset($data[$key]);
                    }   //如果关联的会员已产生佣金的订单全部没有则删除此数据
                }
            }
        }

        if ($data) {
            $opt = [];
            foreach ($data as $key => &$item) {
                $opt[$key]['nickname'] = $item->member->nickname;   //会员昵称
                $opt[$key]['total_brokerage'] = '0.00';   //会员总佣金初始化
                if ($item->member_distribution_order) {
                    foreach ($item->member_distribution_order as $k => $_item) {
                        if (!is_null($_item->memberOrder)) {   //没关联上的不取
                            $opt[$key]['list'][$k]['order_time'] = $_item->create_time;   //订单生成时间
                            $opt[$key]['list'][$k]['pay_time'] = date('Y-m-d', $_item->memberOrder->pay_time);   //订单生成时间
                            $opt[$key]['list'][$k]['price'] = $_item->memberOrder->price;   //订单金额
                            $opt[$key]['list'][$k]['brokerage'] = $_item->brokerage;   //产生佣金
                            $opt[$key]['total_brokerage'] = bcadd($opt[$key]['total_brokerage'], $_item->brokerage, 2);
                        }
                    }
                }
            }

            $opt = array_map(function ($s) {
                $s['list'] = array_values($s['list']);
                return $s;
            }, $opt);

        }

        // return json($opt)

        return $this->fetch('', [
            'mate_head' => '分销订单',
            'data'      => $opt
        ]);
    }
}